# models/audit.py
from datetime import datetime
# from app import db
import uuid
from flask import current_app
from datetime import datetime


# def get_db():
#     return current_app.extensions['sqlalchemy'].db

# db = get_db()


class AuditLog:
    def __init__(self, user_id, event_type, ip_address, user_agent=None):
        self.user_id = user_id
        self.event_type = event_type
        self.ip_address = ip_address
        self.user_agent = user_agent
        self.timestamp = datetime.now()
        # id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
        # user_id = db.Column(db.String(36), db.ForeignKey('user.id'), nullable=False)
        # event_type = db.Column(db.String(50), nullable=False)
        # object_type = db.Column(db.String(50), nullable=False)
        # object_id = db.Column(db.String(36), nullable=False)
        # data = db.Column(db.JSON, nullable=True)
        # old_data = db.Column(db.JSON, nullable=True)
        # timestamp = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
    

    def save(self):
        with current_app.app_context():
            db = current_app.extensions['sqlalchemy'].db
            db.session.add(self)
            db.session.commit()
    #def __repr__(self):
    #    return f"<AuditLog {self.event_type} on {self.object_type}>"